package com.bbs.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.bbs.bean.Topic;
import com.bbs.util.MysqlDB;

public class ControllerCreateTopic extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest
	 * , javax.servlet.http.HttpServletResponse)
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		HttpSession session = request.getSession(true);
		int userId = ((Integer) session.getAttribute("userId")).intValue();
		Topic topic = new Topic();
		request.setCharacterEncoding("UTF-8");
		request.setAttribute("topic", topic);

		String backNews = "";
		String topicTitle = request.getParameter("topicTitle").trim();
		String topicContent = request.getParameter("topicContent").trim();
		
		//中共 民主 共产党 中国共产党 胡锦涛 反共 反党 反国家 习近平 李克强
		//游行 集会 强奸 走私 强暴 摇头丸 白粉 冰毒 海洛因 假钞 人民币 假币
		ArrayList<String> word = new ArrayList<String>();
		word.add("中共");
		word.add("民主");
		word.add("共产党");
		word.add("中国共产党");
		word.add("胡锦涛");
		word.add("温家宝");
		word.add("习近平");
		word.add("李克强");
		word.add("反共");
		word.add("反党");
		word.add("反国家");
		word.add("游行");
		word.add("集会");
		word.add("强奸");
		word.add("走私");
		word.add("强暴");
		word.add("摇头丸");
		word.add("白粉");
		word.add("冰毒");
		word.add("海洛因");
		word.add("假钞");
		word.add("人民币");
		word.add("假币");
		
		
		if(word.contains(topicContent) || word.contains(topicTitle)){
			String logdate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
			String content = "创建新帖的时候输入了敏感词汇";
			try {
				Connection conn = MysqlDB.getConnection();
				Statement stmt = MysqlDB.createStatement(conn);
				int num = stmt.executeUpdate("insert into log(userId,logdate,content) values("+userId+",'"+logdate+"','"+content+"')");
				if(1 == num){
					System.out.println("插入成功!");
				}else{
					System.out.println("插入失败!");
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
			response.sendRedirect("index.jsp");
		}else{
			String nowDate=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
			if (topicTitle == null) {
				topicTitle = "";
			}
			if (topicContent == null) {
				topicContent = "";
			}
			try {
				Connection conn = MysqlDB.getConnection();
				Statement stmt = MysqlDB.createStatement(conn);
				String sql = "insert into topic(topicTitle,topicContent,topicTime,userId) values ('"
						+ topicTitle + "','" + topicContent+ "','" + nowDate + "','" + userId + "')";
				int ret = MysqlDB.executeUpdate(stmt, sql);
				if (ret != 0) {
					backNews = "成功";
					topic.setBackNews(backNews);
					topic.setTopicTitle(topicTitle);
					topic.setTopicContent(topicContent);
					topic.setTopicTime(nowDate);
				}
				MysqlDB.close(stmt);
				MysqlDB.close(conn);
			} catch (Exception e) {
				backNews = "" + e;
				topic.setBackNews(backNews);
			}
			response.sendRedirect("index.jsp");
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
	}

}
